<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org"
>
<!--        核准报表        -->

<head>

    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/css/custom.form.css">

</head>
<style>
    .cx>div{
        margin-bottom: 10px;
    }
</style>
<body>

<div class="panel panel-default operation_dept" hidden>
    <div class="panel-heading title"></div>
    <div class="layui-card-body">
        <form class="layui-form layui-form-pane" action="" lay-filter="deptInfo" style="width: 700px;margin-top: 10px">
            <input name="id" hidden/>
            <input name="aid" hidden/>
            <div class="layui-form-item">
                <div class="layui-form-label ">登记机关</div>
                <div class="layui-input-block">
                    <input type="registration_authority" name="registration_authority" placeholder="请输入登记机关" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-form-label ">管辖机关</div>
                <div class="layui-input-block">
                    <input type="domination" name="domination" placeholder="请输入管辖机关" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label ">地址</label>
                <div class="layui-input-block">
                    <input type="address" name="address" placeholder="请输入地址" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">保存</button>
                    <button class="layui-btn layui-btn-primary" id="btn_cancel">返回</button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="dept-table">
    <!--  ---------------------------------搜索条件  -->
    <div id="searchParam" >
        <div class="layui-form-item cx">
            <div class="layui-input-inline">
                <input type="text" id="registration_authority" class="layui-input" autocomplete="off" placeholder="请输入登录机关">
            </div>
            <div class="layui-input-inline">
                <input type="text" id="domination" class="layui-input" autocomplete="off" placeholder="请输入管辖机关">
            </div>
            <div class="layui-input-inline">
                <input type="text" id="address" class="layui-input" autocomplete="off" placeholder="请输入地址">
            </div>
            <div class="layui-input-inline">
                <input type="text" readonly class="layui-input" id="create_time" placeholder="创建日期">
            </div>
            <div class="layui-input-inline ">
                <button class="layui-btn" onclick="search()" id="search">查询</button>
            </div>
        </div>

    </div>

    <div class="upload-report" hidden>
        <div style="height: 40px;background-color: #009688">
            <span style="line-height: 40px;color: white;letter-spacing: 5px;margin-left: 5px">上传类型</span>
            <a class="layui-btn" href="javascript:;" onclick="uploadClose()" style="width: 40px;float: right">
                <i style="margin-right: -8px;" class="layui-icon layui-icon-close"></i>
            </a>
        </div>
        <div class="layui-input-inline layui-form" style="margin-top: 20px;margin-left: 25%;">
            <select id="report_status_excel">
                <option value="1">全量上传</option>
                <option value="2">增量上传</option>
            </select>
        </div>
        <div style="margin-left: 25%;margin-top: 20px;">
            <button style="width: 212px;height: 40px;letter-spacing: 5px;" class="layui-btn layui-btn-sm" id="uploadAnnualExcel">选择文件</button>
        </div>
    </div>

    <!--  ------------------------------表格  -->
    <table class="layui-table" id="annualsTable" lay-filter="annualsTable"></table>
</div>
<div id="deptTree" style="display: none"></div>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addDept" >添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="batchDeleted" >删除</button>
        <button class="layui-btn layui-btn-sm" lay-event="uploadAnnual" id="uploadAnnual" style="float: right;margin-right: -118px;" >
            上传
        </button>
    </div>

</script>
<script type="text/html" id="tool">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>
    //获取token
    var token = CoreUtil.getData("access_token");
    //地址栏转义token中的#号
    var tokenQuery = token.replace("#", "%23");
    var tableIns2;
    var uploadIns;
    var table = layui.table;
    var upload = layui.upload;
    var form = layui.form;
    var layer = layui.layer;
    var $ = jQuery = layui.jquery;
    var laydate = layui.laydate;
    var tree = layui.tree;
    var transfer = layui.transfer;
    var deptNo = null;
    var createStartTime = null;
    var createEndTime = null;
    layui.use(['table', 'layer', 'laydate', 'tree', 'transfer','upload'], function () {
        tableIns2 = table.render({
            elem: '#annualsTable'// 表格id
            , contentType: 'application/json'
            , headers: {"authorization": token}
            , page: true //开启分页
            , limits: [10,100,1000,10000]
            , limit: 10 //每页默认显示的数量
            , url: '/approvalRpt/findAllApprovalReport' //数据接口
            , method: 'POST'
            , toolbar: '#toolbar'
            , defaultToolbar: []
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                console.log(res);
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": CoreUtil.isEmpty(res.data.list) ? 0 : res.data.total, //解析数据长度
                    "data": CoreUtil.isEmpty(res.data.list) ? null : res.data.list //解析数据列表
                }
            },
            cols: [ //标题栏
                [
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'registrationAuthority', title: '登录机关', width: 300},
                    {field: 'domination', title: '管辖机关', width: 200},
                    {field: 'address', title: '地址'},
                    {
                        field: 'createTime', title: '创建日期',width: 150, templet: function (item) {
                            return "<div>"+layui.util.toDateString(item.createTime, 'yyyy年MM月dd日')+"</div>";
                        }
                    },
                    {title: '操作', width: 150, toolbar: '#tool'}
                ]
            ]
        });

        //上传事件
        uploadIns = upload.render({
            elem: '#uploadAnnualExcel'
            , before: function (obj) {
                this.data = {'uploadType' : $("#report_status_excel").val()}
            }
            ,headers: {"authorization": token}
            ,url: '/approvalRpt/approvalReportUploadExcel'
            ,size: '3072'
            ,accept: 'file' //普通文件
            ,exts: 'xlsx|xls' //允许上传的文件后缀
            , method: 'post'
            ,done: function(res){ //返回值接收
                console.log(res);
                $(".upload-report").hide();
                if(res.data=="1"){
                    layer.msg('导入成功！', {
                    }, function(){
                        location.reload();
                    });
                }else{
                    layer.msg('导入失败！', {
                    }, function(){
                        location.reload();
                    });
                }
            }
        });

        //操作项事件
        table.on('tool(annualsTable)', function (obj) {
            console.log(obj,111);
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    var annualIds = [];
                    annualIds.push(data.id);
                    tipDialog(annualIds, "("+data.address+")");
                    break;
                case 'edit':
                    $(".dept-table").hide();
                    $(".operation_dept").show();
                    $(".title").html("编辑地址");
                    $(".operation_dept input[name=id]").val(data.id);
                    $(".operation_dept input[name=registration_authority]").val(data.registrationAuthority);
                    $(".operation_dept input[name=domination]").val(data.domination);
                    $(".operation_dept input[name=address]").val(data.address);
                    form.render(); //更新全部
                    initTree(data.id);
                    break;
            }
        });
        table.on('toolbar(annualsTable)', function (obj) {
            switch (obj.event) {
                case 'batchDeleted':
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    if (data.length == 0) {
                        layer.msg("请选择要批量删除的地址");
                    } else {
                        var annualIds = [];
                        $(data).each(function (index, item) {
                            annualIds.push(item.id);
                        });
                        tipDialog(annualIds, "选中的");
                    }
                    break;
                case 'addDept':
                    selectNode = null;
                    $(".dept-table").hide();
                    $(".operation_dept").show();
                    $(".title").html("新增地址");
                    $(".operation_dept input[name=registration_authority]").val("");
                    $(".operation_dept input[name=domination]").val("");
                    $(".operation_dept input[name=address]").val("");
                    var x = document.getElementsByClassName("layui-unselect layui-form-switch");
                    x[0].setAttribute("class", "layui-unselect layui-form-switch layui-form-onswitch");
                    var d = document.getElementsByTagName('em')[0];
                    d.firstChild.nodeValue = '启用';
                    form.render(); //更新全部
                    initTree("");
                    break;
                case 'uploadAnnual':
                    uploadAnnual();
                    break;
                case 'downloadAnnual':
                    downloadAnnual();
                    break;
            }
        });

        /**
         * 点击上传按钮弹出选择框
         * 选择框选择是全量上传还是增量上传
         */
        var uploadAnnual = function (){
            $(".upload-report").show();
        };

        /**
         * 下载按钮调用接口
         */
        var downloadAnnual = function () {
            CoreUtil.sendPost("/sys/download", {}, function (res) {
                console.log(res);
            });
        };

        /**
         * 用户新增修改的提交按钮
         * 通过id是否为空判断调用不同的接口
         */
        form.on('submit(submit)', function (data) {
            console.log(data);
            if (data.field.id === undefined || data.field.id === null || data.field.id === "") {
                CoreUtil.sendPost("/approvalRpt/addApprovalReport", data.field, function (res) {
                    if(res.data=="1"){
                        layer.msg('添加成功！', {
                        }, function(){
                            $(".dept-table").show();
                            $(".operation_dept").hide();
                            location.reload();
                        });
                    }else{
                        layer.msg('添加失败！', {
                        }, function(){
                            $(".dept-table").show();
                            $(".operation_dept").hide();
                            location.reload();
                        });
                    }
                });
            } else {
                CoreUtil.sendPut("/approvalRpt/updateApprovalReport", data.field, function (res) {
                    if(res.data=="1"){
                        layer.msg('编辑成功！', {
                        }, function(){
                            $(".dept-table").show();
                            $(".operation_dept").hide();
                            location.reload();
                        });
                    }else{
                        layer.msg('编辑失败！', {
                        }, function(){
                            $(".dept-table").show();
                            $(".operation_dept").hide();
                            location.reload();
                        });
                    }
                });
            }
            return false;
        });

        /**
         * 批量删除和单个删除统一调用接口
         * @param annualIds 选择删除的id数组
         * @param enterpriseName 选择删除的id的企业名
         */
        var tipDialog = function (annualIds, address) {
            layer.open({
                content: '确定要删除' + address + "地址么?",
                yes: function (index, layero) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendDelete("/approvalRpt/delApprovalReport", annualIds, function (res) {
                        if(res.data=="1"){
                            layer.msg('删除成功！', {
                            }, function(){
                                search();
                            });
                        }else{
                            layer.msg('删除失败！', {
                            }, function(){
                                search();
                            });
                        }
                    });
                }
            });
        };

        /**
         * 返回按钮
         */
        $("#btn_cancel").click(function () {
            $(".dept-table").show();
            $(".operation_dept").hide();
            return false;
        });

        /**
         * 设置树形数据
         */
        var loadDeptTree = function (data) {
            console.log(data,123);
            tree.render({
                elem: '#deptTree'
                , data: data
                , onlyIconControl: true  //是否仅允许节点左侧图标控制展开收缩
                , click: function (obj) {
                    selectNode = obj;
                    layer.msg(JSON.stringify(selectNode.data.title));
                }
            });
        };

        /**
         * 切换按钮时修改当前的value值
         */
        form.on('switch(jobSwitch)', function () {
            $(".operation_dept input[name=enterprise_status]").attr('type', 'hidden').val(this.checked ? 1 : 2);
        });

    });

    /**
     * 设置创建日期
     */
    laydate.render({
        elem: '#create_time'
        , type: 'date'
        , range: '~'
        , done: function (value, date, endDate) {
            if (value != null && value != undefined && value != "") {
                createStartTime = value.split("~")[0];
                createEndTime = value.split("~")[1];
            } else {
                createStartTime = null;
                createEndTime = null;
            }

        }
    });

    /**
     * 隐藏选择框
     */
    function uploadClose() {
        $(".upload-report").hide();
    }

    /**
     * 条件查询按钮方法
     */
    function search() {
        //这里以搜索为例
        tableIns2.reload({
            where: { //设定异步数据接口的额外参数，任意设
                registrationAuthority: $("#registration_authority").val(),
                domination: $("#domination").val(),
                address: $("#address").val(),
                createStartTime: createStartTime,
                createEndTime: createEndTime
            }
            , page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    }
</script>
</body>
</html>